Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python提取amazon产品链接_Python_Amazon Web Services - Fatal编程技术网

如何使用python提取amazon产品链接

如何使用python提取amazon产品链接,python,amazon-web-services,Python,Amazon Web Services,我是Python的初学者,我只想从amazon页面删除产品链接。 例如,我想放弃这一页 我在python中使用了这段代码 from bs4 import BeautifulSoup import requests url = "http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368" r = re

我是Python的初学者,我只想从amazon页面删除产品链接。 例如,我想放弃这一页 我在python中使用了这段代码

from bs4 import BeautifulSoup
import requests
url = "http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368"
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")

file = open("parseddata.txt", "wb")

links = soup.find_all('a', {'class': 'a-link-normal s-access-detail-page a-text-normal'})

for link in links:
print(link.get('href'))
file.write(href + '\n')
file.close()

我只希望产品标题链接作为输出。谁能告诉我哪里做错了。

在请求
标题中添加
用户代理
,假装你不是机器人

from bs4 import BeautifulSoup
import requests
url = "http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368"

# add header
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")

file = open(r"parseddata.txt", "w")

links = soup.find_all('a', {'class': 'a-link-normal s-access-detail-page a-text-normal'})

for link in links:
    print(link.get('href'))
    file.write(link.get('href')+ '\n')
file.close()
结果
您希望您的代码做什么?它实际做什么?您是否收到任何错误消息或警告?结果是错误的吗?如果是,在什么方面?@Gurpeet Singh你不应该这样做(如果是为了一些严重的事情),希望你知道亚马逊有一个面向开发者的api?
https://www.amazon.com/Funko-POP-Marvel-Dancing-Bobble/dp/B00N1EJXUU/ref=sr_1_1/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-1&refinements=p_4%3AFunKo
https://www.amazon.com/Funko-POP-Movies-Potter-Action/dp/B019JIA4IQ/ref=sr_1_2/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-2&refinements=p_4%3AFunKo
https://www.amazon.com/FunKo-2390-Funko-Darth-Maul/dp/B005F1QBMK/ref=sr_1_3/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-3&refinements=p_4%3AFunKo
........