更新 1:
当 op 编辑他的帖子并以他的代码格式修复空格时,我正在更新我的答案。
当程序请求链接时,写下(或粘贴)您的链接并在按回车之前添加一个空格。
输入末尾的多余空格将阻止 IDE 在浏览器窗口中打开链接,而不是使用返回键结束输入,因此它将按预期继续执行输入函数后的下一个代码。
这样做,您的代码实际上可以正常工作,正如我在第一个答案中所展示的那样。
我之前的回答:
我注意到你在这里有不正确的空格'a-color-base'
替换这一行:
linkElems = soup.select('div.a-row a.a-size-base.a-link-normal.review-title.a- color-base.a-text-bold')
与
linkElems = soup.select('div.a-row a.a-size-base.a-link-normal.review-title.a-color-base.a-text-bold')
额外:
目前您的代码也仅适用于 amazon.com ,要使您的代码适用于其他亚马逊网站,如 amazon.in amazon.co.uk,您需要修改此行:
webbrowser.open('https://amazon.com' + linkElems[i].get('href'))
类似于:
from urllib.parse import urlparse
url_components = urlparse(url)
webbrowser.open('https://' + url_components.netloc + linkElems[i].get('href'))
现在它可以与 amazon.in 等其他亚马逊网站正常工作,而不仅仅是 amazon.com,试试吧。
试驾:
Enter the url: https://www.amazon.in/Intex-PB-16K-Poly-16000mAH-Lithium/dp/B07843GH8X/ref=cm_cr_srp_d_product_top?ie=UTF8
2019-02-15 22:51:28,940 - DEBUG - Starting new HTTPS connection (1): www.amazon.in:443
2019-02-15 22:51:30,125 - DEBUG - https://www.amazon.in:443 "GET /Intex-PB-16K-Poly-16000mAH-Lithium/dp/B07843GH8X/ref=cm_cr_srp_d_product_top?ie=UTF8%20 HTTP/1.1" 200 None
2019-02-15 22:51:32,019 - DEBUG - [<a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R123ICSCDM2MF3/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Nice product.</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R3OTWE19SMPPVQ/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Not a good product. It takes a day to charge the ...</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R1Z51ERFCD7D6P/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">not portable easily..</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R24GYC4HRBGTM1/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Five Stars</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R2P5ILE8KQF8PJ/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Fine not superb</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R3C9ZYFRT9NWAK/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Quality & Performance</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R16F4OE3LWHHQI/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Worst experience. Don’t buy</a>, <a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R26ROATK8PU6TL/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X">Very bad product</a>]
2019-02-15 22:51:32,019 - DEBUG - Link is: /gp/customer-reviews/R123ICSCDM2MF3/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X
2019-02-15 22:51:32,295 - DEBUG - Link is: /gp/customer-reviews/R3OTWE19SMPPVQ/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X
2019-02-15 22:51:32,628 - DEBUG - Link is: /gp/customer-reviews/R1Z51ERFCD7D6P/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X
2019-02-15 22:51:32,933 - DEBUG - Link is: /gp/customer-reviews/R24GYC4HRBGTM1/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X
2019-02-15 22:51:33,302 - DEBUG - Link is: /gp/customer-reviews/R2P5ILE8KQF8PJ/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07843GH8X
如果您的调试函数抛出错误:
我不确定您的实际代码中的这个空间是否存在,或者它在此处以错误的帖子格式显示,但无论如何您应该从格式 arg '% (levelname)s' 中删除空格,以便调试按预期工作:
替换这个:
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - % (levelname)s - %(message)s')
与
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
希望这会对你有所帮助。