2016-08-22 61 views
-2
import requests 
from bs4 import BeautifulSoup 

''' 
It's a web crawler working in ebay, collecting every single item data 
''' 

def ebay_spider(max_pages): 
    page = 1 
    while page <= max_pages: 
     url = 'http://www.ebay.co.uk/sch/Apple-Laptops/111422/i.html?_pgn=' \ 
       + str(page) 
     source_code = requests.get(url) 

     plain_text = source_code.text 
     soup = BeautifulSoup(plain_text) 
     for link in soup.findAll('a', {'class': 'vip'}): 
      href = 'http://www.ebay.co.uk' + link.get('href') 
      title = link.string 
    get_single_item_data(href) 
    page += 1 


def get_single_item_data(item_url): 
    source_code = requests.get(item_url) 
    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text) 
    for item_name in soup.findAll('h1', {'id': "itemTitle"}): 
     print(item_name.string) 

ebay_spider(3) 

Blockquote And the error say that : http://imgur.com/403a6N8
I tried to fix it but it seems not to work, so any tips/answers how to fix it?的Python BS4模块

EDIT: Sorry everyone for faulty title and tag, everything was fixed.

+0

你试过它告诉你什么? 'soup = BeautifulSoup(plain_text,“html.parser”,markup_type = markup_type)'。并请发布错误的文本版本,而不是一个无法读取的图像。 –

+0

这与'requests'模块无关。 – DeepSpace

+0

@让 - 弗朗索瓦法布尔对不起照片的配偶感到遗憾,但是你误会了错误。但问题是我将该行粘贴到我的代码中,并且出现如下错误:SyntaxError:标识符中的无效字符。由于一些奇怪的原因,我找不到它有什么问题。这是前面的错误,是什么帖子是关于:http://pastebin.com/HNL1ENG0 – Auginis

回答

1

当你试图在线路BeatifulSoup对象,做的却是:

soup = BeautifulSoup(plain_text) 

此:

soup = BeautifulSoup(plain_text, 'html.parser') 

注意:你的问题是指BS4模块,而不是要求。

+0

对不起,先生,错误的标题和标签(我的坏)抱歉,并感谢您的回答,但然后我写这条线说:SyntaxError:标识符中的无效字符。 imgur.com/a/8NBDi – Auginis

+0

如果您没有指定markup_type,请执行以下操作:soup = BeautifulSoup(plain_text,'html.parser')而不是:soup = BeautifulSoup(plain_text,'html.parser',markup_type = markup_ty pe )。如果我的回复很有用,请将其标记为有用。 – dannyxn

+0

我已经改变了我的答案,以满足您的需求。我建议你看看:https://www.crummy.com/software/BeautifulSoup/bs4/doc/。 – dannyxn

0

这是完全无关的请求模块。正如让 - 弗朗索瓦所说的那样,做它告诉你的事情并继续前进。

soup = BeautifulSoup(plain_text,"html.parser",markup_type=markup_ty‌​pe)

+0

对不起,先生,对于错误的标题和标签(我的坏)nd谢谢你的回答,但然后我写这一行它说:SyntaxError :标识符中的字符无效。 http://imgur.com/a/8NBDi – Auginis