爬取一页商品数据

爬取一页商品数据_第1张图片
![要求.png](http://upload-images.jianshu.io/upload_images/1706760-631a431588d568f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

以下是实现的代码



from bs4 import BeautifulSoup
import requests
'''
def get_list_links():
    num = input('page_num:')
    for i in range(1, int(num) + 1):
        list_urls = 'http://bj.58.com/pbdn/0/pn{}/'.format(i)
        list_htmls = requests.get(list_urls)
        list_soup = BeautifulSoup(list_htmls.text, 'lxml')
        # print(list_soup) 没问题
        list_links = list_soup.select('td.t > a.t')
        # print(list_links) 没问题
        for list_link in list_links:
            link = list_link.get('href').split('?')[0]
        return link
        detail_html = requests.get(link)
        detail_soup = BeautifulSoup(detail_html.text, 'lxml')
        title = detail_soup.select('div.col_sub h1')[0].get_text()
        print(title)
get_list_links()
'''

url = 'http://bj.58.com/pbdn/0/'
html = requests.get(url)
#print(html)
bsObj = BeautifulSoup(html.text, 'lxml')
#print(bsObj)
list_links = bsObj.select('td.t > a.t')
for list_link in list_links:
    print(list_link)

你可能感兴趣的:(爬取一页商品数据)