NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

“C:\Program Files\Python36-32\python.exe” C:/Users/Administrator/PycharmProjects/untitled/getiFanxu.py
[<h1 class=”entry-title”>支付宝扫码领红包</h1>]
Traceback (most recent call last):
File “C:/Users/Administrator/PycharmProjects/untitled/getiFanxu.py”, line 21, in <module>
img = soup.select(‘#post-2266 > div > p:nth-child(1) > a > img’)
File “C:\Program Files\Python36-32\lib\site-packages\bs4\element.py”, line 1532, in select
for candidate in _use_candidate_generator(tag):
File “C:\Program Files\Python36-32\lib\site-packages\bs4\element.py”, line 1493, in recursive_select
for i in tag.select(next_token, recursive_candidate_generator):
File “C:\Program Files\Python36-32\lib\site-packages\bs4\element.py”, line 1451, in select
‘Only the following pseudo-classes are implemented: nth-of-type.’)
NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

Process finished with exit code 1

解决办法为:nth-child改为nth-of-type 

Use nth-of-type instead of nth-child

urllib.error.HTTPError: HTTP Error 403: Forbidden

urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出”HTTP Error 403: Forbidden“异常

“C:\Program Files\Python36-32\python.exe” C:/Users/Administrator/PycharmProjects/untitled/t.py
Traceback (most recent call last):
File “C:/Users/Administrator/PycharmProjects/untitled/t.py”, line 10, in <module>
data1 = urllib.request.urlopen(download_img).read()
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 223, in urlopen
return opener.open(url, data, timeout)
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 532, in open
response = meth(req, response)
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 642, in http_response
‘http’, request, response, code, msg, hdrs)
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 570, in error
return self._call_chain(*args)
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 504, in _call_chain
result = func(*args)
File “C:\Program Files\Python36-32\lib\urllib\request.py”, line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Process finished with exit code 1

如果用 urllib.request.urlopen 方式打开一个URL,服务器端只会收到一个单纯的对于该页面访问的请求,但是服务器并不知道发送这个请求使用的浏览器,操作系统,硬件平台等信息,而缺失这些信息的请求往往都是非正常的访问,例如爬虫.
有些网站为了防止这种非正常的访问,会验证请求信息中的UserAgent(它的信息包括硬件平台、系统软件、应用软件和用户个人偏好),如果UserAgent存在异常或者是不存在,那么这次请求将会被拒绝(如上错误信息所示)
所以可以尝试在请求中加入UserAgent的信息
如下:

继续阅读“urllib.error.HTTPError: HTTP Error 403: Forbidden”

AttributeError: module ‘urllib’ has no attribute ‘urlopen’

“C:\Program Files\Python36-32\python.exe” C:/Users/Administrator/PycharmProjects/untitled/t.py
Traceback (most recent call last):
File “C:/Users/Administrator/PycharmProjects/untitled/t.py”, line 9, in <module>
data=urllib.urlopen (req).read ( )
AttributeError: module ‘urllib’ has no attribute ‘urlopen’

Process finished with exit code 1

Python3.X中应该用urllib.request

分析:红色的部分表示找不到,一般出现这样的问题有这么几种方式

① 没有引入对应的包,毕竟python的包多~~

② 还有工程目录下可能有一个自己定义的文件与urllib重名,导致上述代码在引用时实际引用的是自定义的那个urllib

data=urllib.urlopen (req).read ( )

data=urllib.request.urlopen (req).read ( )