python 파충류--약망을 기어오르는 약품 정보-excel표에 쓰기

10983 단어 파충류
import requests
from lxml import etree
import xlwt
def write_to_excel(infos):
    # workbook
    workbook = xlwt.Workbook(encoding='utf-8')
    # sheet 
    sheet = workbook.add_sheet('word_list')
    #  
    head =[]
    keys = infos[0].keys()
    # 
    for key in keys:
        head.append(key)
    # 
    for i in range(len(head)):
        sheet.write(0,i,head[i])
    i = 1# , 
    for item in infos:
        for j in range(len(head)):
            sheet.write(i,j,item[head[j]])
        i+=1
    # 
    workbook.save('word.xls')
    print(' !')

def get_text(text):
    if text:
        return text[0]
    return ''
def parse_page(html_str):
    tree = etree.HTML(html_str)
    li_list = tree.xpath('//ul[@id="itemSearchList"]/li')
    ## , , , 
    for li in li_list:
        # print(li)
        price = get_text(li.xpath('.//div[@isrecom="0"]/p[1]/textarea/span/text()|.//div[@isrecom="0"]/p[1]/span/text()|.//div[@isrecom="0"]/p[1]/span/u/text()')).strip()
        name = li.xpath('.//div[@isrecom="0"]/p[2]/a/text()')[1].strip()
        comment = get_text(li.xpath('.//div[@class="sell_type_div"]/span/a/em/text()')).strip()
        url = get_text(li.xpath('.//div[@class="itemSearchResultCon"]/a[1]/@href')).strip()

        item = {}
        item['price'] = price
        item['name'] = name
        item['comment'] = comment
        item['url'] = 'https:' + url
        print(item)
        infos.append(item)
def main():
    base_url = 'https://www.111.com.cn/categories/953710-a0-b0-c31-d0-e0-f0-g0-h0-i0-j%s.html'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
    }
    for i in range(1,10):
        response = requests.get(base_url %i,headers=headers)
        parse_page(response.text)


if __name__ == '__main__':
    infos = []
    main()
    write_to_excel(infos)


좋은 웹페이지 즐겨찾기