当前位置:网站首页>已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
2022-07-03 12:34:00 【无 羡ღ】
成功解决:TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
报错代码
我的代码:
import requests
import time
import random
from lxml import etree
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}
def baidu_search(search_keyword, max_page):
for page in range(max_page):
time.sleep(random.randint(1, 2))
url = 'https://www.baidu.com/s?wd=%s' % search_keyword + '&pn=' + str(page * 10)
r = requests.get(url, headers=headers)
html = r.text
print(html)
html_data = etree.HTML(html, etree.HTMLParser)
# r1 = html_data.xpath('//h3')
# r2 = html_data.xpath('//*[@class="c-abstract"]')
# r3 = html_data.xpath('//*[@class="t"]/a/@href')
#
# for i in range(8):
# r11 = r1[i].xpath('string(.)')
# r22 = r2[i].xpath('string(.)')
# r33 = r3[i]
# print(r11)
def main():
rows = [('吾皇万睡'), ('Nike')]
max_page = 1
for search_keyword in rows:
baidu_search(search_keyword, max_page)
if __name__ == '__main__':
main()
报错内容:
Traceback (most recent call last):
File "E:/Python/3.py", line 54, in <module>
main()
File "E:/Python/3.py", line 46, in main
baidu_search(search_keyword, max_page)
File "E:/Python/3.py", line 29, in baidu_search
html_data = etree.HTML(html,etree.HTMLParser)
TypeError: Argument 'parser' has incorrect type (expected lxml.etree._BaseParser, got type)

报错原因
报错内容翻译:TypeError:参数“parser”的类型不正确(应为lxml.etree。\u BaseParser,get type)
解决方法
把解析代码换成即可:
html_data = etree.HTML(html)
边栏推荐
- mysqlbetween实现选取介于两个值之间的数据范围
- Brief introduction to mvcc
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
- When we are doing flow batch integration, what are we doing?
- Some thoughts on business
- 【习题五】【数据库原理】
- Flink SQL knows why (13): is it difficult to join streams? (next)
- 解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
- Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
猜你喜欢

Image component in ETS development mode of openharmony application development

Leetcode234 palindrome linked list
![[data mining review questions]](/img/96/00f866135e06c4cc0d765c6e499b29.png)
[data mining review questions]

Deeply understand the mvcc mechanism of MySQL

人身变声器的原理

【Colab】【使用外部数据的7种方法】

When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution

2022-02-09 survey of incluxdb cluster

【R】【密度聚类、层次聚类、期望最大化聚类】

Analysis of the influence of voltage loop on PFC system performance
随机推荐
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Detailed explanation of the most complete constraintlayout in history
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
剑指 Offer 17. 打印从1到最大的n位数
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
【判断题】【简答题】【数据库原理】
【R】【密度聚类、层次聚类、期望最大化聚类】
Sword finger offer 15 Number of 1 in binary
【习题五】【数据库原理】
35道MySQL面试必问题图解,这样也太好理解了吧
sitesCMS v3.0.2发布,升级JFinal等依赖
2022-02-10 introduction to the design of incluxdb storage engine TSM
2022-02-09 survey of incluxdb cluster
Export the entire Oracle Database
SQL learning notes (I)
Server coding bug
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
2022-02-11 practice of using freetsdb to build an influxdb cluster
Seven habits of highly effective people
context. Getexternalfilesdir() is compared with the returned path